/*
 * Copyright (c) 2016-2017 fulihui.com All right reserved
 * Project: integral-business-parent
 * File name: Selector.java
 */

package com.fulihui.integralbusiness.handler;

import com.fulihui.integralbusiness.constants.IntegralBusinessFlags;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.ApplicationObjectSupport;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Willard.Hu on 2017/4/21 0021.
 */
public class IntegralBusinessSelector extends ApplicationObjectSupport {
    private transient Logger log = LoggerFactory.getLogger(getClass());

    private Map<IntegralBusinessFlags, IntegralBusinessHandler> handlers = new HashMap<>();

    @PostConstruct
    void init() {
        Map<String, AbstractIntegralBusinessHandler> beansOfType = getApplicationContext()
                .getBeansOfType(AbstractIntegralBusinessHandler.class);
        beansOfType.values().forEach(it -> handlers.put(it.getFlag(), it));
        log.info("Registered handlers size: {}", handlers.size());
    }

    public IntegralBusinessHandler getHandler(IntegralBusinessFlags flag) {
        return handlers.get(flag);
    }
}
